package lanbo.cj.lmp.activity;

import android.content.SharedPreferences;
import android.widget.*;
import lanbo.cj.lmp.R;
import lanbo.cj.lmp.bean.User;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import cn.bmob.im.BmobUserManager;
import cn.bmob.v3.listener.UpdateListener;

public class InfoActivity extends Activity {
	private ImageView mBack;
	private EditText mName, mTel, mEmail, mAge;
	private TextView mKeiShi;
	private Button mBtn;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.info);

		findViewById();
		setListener();

	}

	private void findViewById() {
		mBack = (ImageView) findViewById(R.id.info_back);
		mName = (EditText) findViewById(R.id.info_name);
		mTel = (EditText) findViewById(R.id.info_tel);
		mEmail = (EditText) findViewById(R.id.info_email);
		mAge = (EditText) findViewById(R.id.info_age);
		mBtn = (Button) findViewById(R.id.info_btn);
		mKeiShi = (TextView)findViewById(R.id.info_keShi);
		
		BmobUserManager userManager=BmobUserManager.getInstance(this);
		User user = userManager.getCurrentUser(User.class);
		mName.setHint(user.getName());
		if(user.getTel() != null)mTel.setHint(user.getTel());
		if(user.getEmail() != "")mEmail.setHint(user.getEmail());
		if(user.getAge() != 0)mAge.setHint(user.getAge()+"");
		mKeiShi.setText(getUserKeShiName());
	}

	private void setListener() {
		mBack.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				finish();
			}
		});
		mBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				modifyUser();
			}
		});
	}

	private void modifyUser() {
		User user = new User();
		user.setObjectId(User.getCurrentUser(InfoActivity.this).getObjectId());
		user.setUsername(User.getCurrentUser(InfoActivity.this).getObjectId());
		user.setName(mName.getText().toString().trim());
		user.setTel(mTel.getText().toString().trim());
		user.setEmail(mEmail.getText().toString().trim());
		String age = mAge.getText().toString().trim();
		if (age != null)
			user.setAge(Integer.valueOf(age));
		user.update(InfoActivity.this, new UpdateListener() {
			public void onSuccess() {
				Toast.makeText(InfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
				finish();
			}

			public void onFailure(int arg0, String arg1) {
				Toast.makeText(InfoActivity.this, "修改失败" + arg1, Toast.LENGTH_SHORT).show();
			}
		});
	}
	private String getUserKeShiName(){
		SharedPreferences setPre = this.getSharedPreferences("user", 0);
		return  setPre.getString("keshiName","请输入所属科室");
	}
}
